/*
** server.c for  in /home/faye_m//zappy
** 
** Made by mouhamed faye
** Login   <faye_m@epitech.net>
** 
** Started on  Mon Jun 13 15:12:16 2011 mouhamed faye
** Last update Fri Jul  1 23:03:43 2011 arnaud1 tessa
*/
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <string.h>
#include "server.h"
#include "team.h"
/*
void	welcome(Vars_Packages *var)
{
  var->sockfd2 = accept(var->sockfd,(struct sockaddr*)
			&var->client, &var->sin_size);	
  printf("\tConnexion etablie avec  a %s\n -----Sending Bienvenue Client %d-------\n",
	       inet_ntoa(var->client.sin_addr), var->sockfd2);
   get_time();
  var->tabsockfd[var->taille] = var->sockfd2;
  var->taille++;
  var->tailleMsg = snprintf(var->buffer, 11, "Bienvenue\n");
  send(var->sockfd2, &var->buffer, var->tailleMsg, 0);
  bzero(var->buffer, strlen(var->buffer));
}


void	disconnect(Vars_Packages *var)
{
      printf("Client Zappy %d a quit the game\n", var->
	     tabsockfd[var->i]);
      close(var->tabsockfd[var->i]);
      var->tabsockfd[var->i] = var->tabsockfd[var->taille - 1];
      var->taille--;
}


void	set_FD(Vars_Packages *var)
{
  FD_ZERO(&var->readfds);
  var->sockmax = var->tabsockfd[0];
  for(var->i = 0 ; var->i < var->taille; var->i++)
    {
      FD_SET(var->tabsockfd[var->i],&var->readfds);
      if(var->sockmax < var->tabsockfd[var->i])
	var->sockmax = var->tabsockfd[var->i];
    }
}

*/


void  	infos_server(char **av,  Vars_Packages  *var)
{
  int i;
  i = 5;
  printf ("\n\tListening on port %d...\n\n", atoi(av[1]));
  printf ("Configuration  : Max(%d)  WorldX(%d) WorldY(%d) T(%d)\n\n",
	  atoi(av[4]), atoi(av[2]), atoi(av[3]), atoi(av[5]));
  printf("Generation Map : in progress .....\n\n");
 
  while (av[i++] != NULL)
    var->list_of_team = add_team(var->list_of_team, av[i], atoi(av[4]));
  display_team(var->list_of_team, atoi(av[4]));
}



int		zappy_server(int port, char **av, Vars_Packages *var)
{
  /*
    -affiche les informations du serveur (team, port, map)
    -accepter connexion
    -lire message client    
    -envoit le message a manage command
    -deconnecter client
   */

  init_socket(var, port);
  var->tabsockfd[0] = var->sockfd;
  var->taille++;
  infos_server(av,  var);

  while(1)
    {
      // set_FD(var);
      FD_ZERO(&var->readfds);
      var->sockmax = var->tabsockfd[0];
      for(var->i = 0 ; var->i < var->taille; var->i++)
	{
	  FD_SET(var->tabsockfd[var->i],&var->readfds);
	  if(var->sockmax < var->tabsockfd[var->i])
	    var->sockmax = var->tabsockfd[var->i];
	}      
      if(select(var->sockmax+1, &var->readfds, NULL, NULL, NULL) == -1)
	perror("select()");
      //welcome
      if(FD_ISSET(var->sockfd, &var->readfds))
	{
	  var->sockfd2 = accept(var->sockfd,(struct sockaddr*)
				&var->client, &var->sin_size);	
	    printf("\tConnexion etablie avec 
	a  %s\n -----Sending Bienvenue Client %d-------\n",
		   inet_ntoa(var->client.sin_addr), var->sockfd2);
	    var->tabsockfd[var->taille] = var->sockfd2;
	    var->taille++;
	    var->tailleMsg = snprintf(var->buffer, 11, "Bienvenue\n");
	    send(var->sockfd2, &var->buffer, var->tailleMsg, 0);
	    bzero(var->buffer, strlen(var->buffer));
	}
      for(var->i=1 ;var->i < var->taille; var->i++)
	{
	  if(FD_ISSET(var->tabsockfd[var->i], &var->readfds))
	    {
	      var->tailleMsg = recv(var->tabsockfd[var->i], &var->buffer, MAXBUFFERSIZE,0);
	      if (var->taille == -1)
		perror("recv()");
	      else if (var->tailleMsg == 0)
		{
		  //disconnect(var);
		  printf("Client Zappy %d a quit the game\n", var->
			 tabsockfd[var->i]);
		  close(var->tabsockfd[var->i]);
		  var->tabsockfd[var->i] = var->tabsockfd[var->taille - 1];
		  var->taille--;
		}
	      else
		manage_cmd(var);
	    }
	}
    }
}
